<?php
class Admin_Form_Image extends  Admin_Form_Abstract
{
     const URL_PATTERN    = '/^(http:\/\/|https:\/\/|www\.)([\w\d\-]+\.){1,}[\w]{2,}(\/[\S]*)*$/';
    
    //static const TYPE = '1';
    public function __construct(array $options = null)
    {   
        
        parent::__construct();
        
    }

    public function init()
    {
        

        $this->setAttrib('class', 'form_image');
        
        $this->addElement(new YouNet_Form_Element_File('image',array(
			'decorators'	=> $this->_file_decorators,
			'label'			=> "Upload",
            'required' => true,
		)));
//        
//        $this->addElement("file", 'image', array(
//            'label' => "Upload" ,
//            'required' => true,
//            'decorators' => $this->_file_decorators
//        ));

        $this->image->setDestination(IMG_PATH);
        
		// ensure only 1 file
		$this->image->addValidator('Count', false, 1);
		// limit to 100K
		$this->image->addValidator('Size', false, 512000);//500kb
		// only JPEG, PNG, and GIFs
		$this->image->addValidator('Extension', false, 'jpg,png,gif');
		//$this->image->setDescription(".jpg|.png|.gif - max 500Kb - preferred width 810px for best fit on Fan Page.");
        
        $this->addElement("hidden", 'hdf_type', array(
            'value'=>'1',
            'decorators' => $this->_hidden_decorators
        ));
         $this->addElement("hidden", 'hdf_check_show', array(
            'value'=>'1',
            'decorators' => $this->_hidden_decorators
        ));
        
        
        $this->addElement("text", 'image_url', array(
            'label' => "Image Link",
            'description' => $this->getTranslator()->translate('DES_IMAGE_URL')
        ));
        $this->image_url->setAttrib('maxlength','255');
       
        $this->image_url->addValidator('regex', true, 
            array(
                'pattern' =>self::URL_PATTERN, 
                'messages'=>array(
                    'regexNotMatch'=>'URL invalid'
                )
            )
        );
        
         $this->getElement('image_url')->addValidator('stringLength', false, array(0, 255));
        
         $this->addElement('submit', 'submit', array(
                'class' => 'button_control greenbutton',
                'value' => 'BUTTON_SAVE',
                'label'=> 'BUTTON_SAVE',
                'decorators' => $this->_button_decorators
            ));
        // $this->submit->removeDecorator('DtDdWrapper'); 
        
        $this->addThumbnail();
    }
    
    public function addThumbnail($image = null)
    {
        $description = sprintf($this->getTranslator()->translate('UPLOAD_IMAGE_HINT'), '500kb');
        $pathImg = IMG_PATH_VIEW;
        if (!empty($image))
        {            
            $description .= '<div class="image_thumbnail">';
            //$description .= '<input type="hidden" name="is_delete_banner"/>';
            $description .= '<img  src="' . $pathImg.'/'.$image.'"/>';                   
            $description .= '</div>';            
        } 
       
        //        
        $this->image->setDescription($description);
        $this->image->getDecorator('Description')->setOption('escape', false);
    }
    
    public function getImageNameAndReceive()
	{
		$ret_name			= '';
		/*@var $imageElement Zend_Form_Element_File*/
		$imageElement				= $this->image;
		if( !empty($imageElement) )
		{
			$image_name			= $imageElement->getFileName(null, false);
             
			if ( !empty($image_name) )
			{
                $name_length = strlen($image_name);
				if($name_length  > 40 )
				{
					$image_name = substr($image_name, 0, 40) . substr($image_name, $name_length - 4, $name_length);
				}
					
				$ret_name	= time() . '_'. mt_rand(0, 9999).'_'. preg_replace('/[^A-Za-z0-9-_.]/', '', $image_name);
				$target		= IMG_PATH;
				
				$target		.= "/". $ret_name;
                
				$result1 = $imageElement->addFilter('Rename',array(
					'target'    => $target
				));
               
			}
		}
		return $ret_name;
	}
}